package com.zzy.chapter3.deprecated.state;

import com.zzy.chapter3.utils.RedisCommonProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DeprecatedPayOrder extends DeprecatedAbstractOrderState {

    @Autowired
    private RedisCommonProcessor redisCommonProcessor;

    @Autowired
    private DeprecatedAbstractOrderState orderState;

    @Override
    protected DeprecatedOrder payOrder(String orderId) {
        // 1. 从redis中取出订单
        DeprecatedOrder order = (DeprecatedOrder) redisCommonProcessor.get(orderId);
        if (!order.getState().equals(ORDER_WAIT_PAY)) {
            throw new RuntimeException("订单状态异常");
        }
        // 2. 支付逻辑

        // 3. 支付完成后，修改订单状态为待发货，并更新redis缓存
        order.setState(ORDER_WAIT_SEND);
        redisCommonProcessor.set(orderId, order);
        // 4. 观察者模式：发送订单支付event
        orderState.notifyObserve(orderId, ORDER_WAIT_SEND);
        // 5. 订单支付完成，设置上下文状态为“待发货”
        return order;
    }
}
